package com.cyt.web.controller.gen;

import com.cyt.base.model.dto.ReturnResult;
import com.cyt.gen.model.vo.GeneratorVo;
import com.cyt.gen.service.GeneratorService;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.servlet.ModelAndView;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;

/**
 * 代码生成
 *
 * @author rong yuan
 * @date 2019/1/28 13:36
 */
@RestController
public class GeneratorController {

    @Resource
    private GeneratorService generatorService;

    /**
     * 跳转代码生成分页界面
     *
     * @return ModelAndView
     * @author rong yuan
     * @date 2019/7/3 10:38
     */
    @RequestMapping(value = "/gen/gen/getList", method = RequestMethod.GET)
    public ModelAndView getList() {
        return new ModelAndView("/gen/gen/genList");
    }

    /**
     * 数据库表分页查询
     *
     * @param generatorVo 传入参数对象
     * @param page        当前页数
     * @param limit       展示数量
     * @author rong yuan
     * @date 2019/7/3 10:46
     */
    @RequestMapping(value = "/gen/gen/pageTable", method = RequestMethod.POST)
    public ReturnResult pageTable(GeneratorVo generatorVo, int page, int limit) {
        return generatorService.pageTable(generatorVo, page, limit);
    }

    /**
     * 跳转代码生成编辑界面
     *
     * @param generatorVo 传入参数对象
     * @author rong yuan
     * @date 2019/7/3 10:46
     */
    @RequestMapping("/gen/gen/getEdit")
    public ModelAndView getEdit(GeneratorVo generatorVo) {
        ModelAndView view = new ModelAndView("/gen/gen/genEdit");
        generatorVo = generatorService.selectTable(generatorVo);
        view.addObject("generatorVo", generatorVo);
        return view;
    }

    /**
     * 生成代码
     *
     * @param generatorVo 传入参数对象
     * @return ReturnResult
     * @author rong yuan
     * @date 2019/7/3 11:09
     */
    @RequestMapping(value = "/gen/gen/generatorCode", method = RequestMethod.POST)
    public ReturnResult generatorCode(HttpServletRequest request, GeneratorVo generatorVo) {
        String[] projectPaths = request.getServletContext().getRealPath("/").split("target");
        String projectPath = projectPaths[0].split(projectPaths[1].replace("\\", ""))[0].replace("\\", "/");
        generatorVo.setProjectPath(projectPath);
        return generatorService.generatorCode(generatorVo);
    }
}